package com.pswv2.backstage.skyset.action;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.omg.CORBA.portable.InputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.aliyun.oss.model.Style;
import com.pswv2.backstage.keyword.service.KWordService;
import com.pswv2.backstage.skyset.service.SkySetService;
import com.pswv2.bub.bsc.dao.po.Details;
import com.pswv2.bub.bsc.dao.po.KWord;
import com.pswv2.bub.bsc.dao.po.User;
import com.pswv2.plane.PlaneUtil;
import com.pswv2.util.BackendErrorCode;
import com.pswv2.util.CommonResp;
import com.pswv2.util.ExtraSpringHibernateTemplate;
import com.pswv2.util.GainTime;
import com.pswv2.util.MusicUtil;
import com.pswv2.util.Paging;
import com.pswv2.util.ReportGenerator;
import com.pswv2.util.Sessions;
import com.pswv2.util.StringUtil;
import com.pswv2.util.Strings;

/**
 * 详细细节
 */
@Controller
@RequestMapping(value = "skySetAction", produces = { "text/json;charset=UTF-8" })
public class SkySetAction {
	private static Class<?> poc = KWord.class;
	@Autowired
	private ExtraSpringHibernateTemplate extraSpringHibernateTemplate;
	@Autowired
	private SkySetService skySetService;

	/**
	 * 需求于目的
	 */
	@ResponseBody
	@RequestMapping(value = { "addNeed" })
	public String addNeed(String need, HttpSession session) {
		Details details=new Details();
		details.setNeed(need);
		extraSpringHibernateTemplate.getHibernateTemplate().save(details);
		return CommonResp.buildSuccessResp().toJsonString();
	}
	
	/**
	 * 添加细节
	 * @param need 需求内容
	 * @param departsite 出发地点
	 * @param termini 出发地
	 * @param startTime 开始时间
	 * @param endtime  抵达时间
	 * @param needres  需要带什么
	 * @param work   需要办什么事
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = { "addDetails" })
	public String addDetails(String need,String departsite,
			String termini,Long startTime,Long takeoff,Long length,
			String needres,String work,HttpSession session) {
		Details details=new Details();
		details.setDepartsite(departsite);
		details.setTakeoff(takeoff);
		details.setNeed(need);
		details.setNeedres(needres);
		details.setStartTime(startTime);
		details.setTermini(termini);
		details.setWork(work);
		details.setLength(length);
		extraSpringHibernateTemplate.getHibernateTemplate().save(details);
		return CommonResp.buildSuccessResp(details).toJsonString();
	}
	/**
	 * 添加细节
	 * @param need 需求内容
	 * @param departsite 出发地点
	 * @param termini 出发地
	 * @param startTime 开始时间
	 * @param endtime  抵达时间
	 * @param needres  需要带什么
	 * @param work   需要办什么事
	 * @param session
	 * @return
	 * @throws ParseException 
	 */
	@ResponseBody
	@RequestMapping(value = { "addAndroidDetails" })
	public String addAndroidDetails(String need,String departsite,
			String termini,String startTime,String takeoff,Long length,
			String needres,String work,Integer userId) throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date dateStart1 = format.parse(startTime);
		Date dateStart2 = format.parse(takeoff);
		Details details=new Details();
		details.setUserId(userId);
		details.setDepartsite(departsite);
		details.setTakeoff(dateStart2.getTime());
		details.setNeed(need);
		details.setNeedres(needres);
		details.setStartTime(dateStart1.getTime());
		details.setTermini(termini);
		details.setWork(work);
		details.setLength(length);
		extraSpringHibernateTemplate.getHibernateTemplate().save(details);
		return CommonResp.buildSuccessResp(details).toJsonString();
	}
	/**
	 * 返回结果
	 */
	@ResponseBody
	@RequestMapping(value = { "getFruit" })
	public String getFruit(int id) {
		Details details=skySetService.queryById(id);
		Calendar calendar = Calendar.getInstance();
		GainTime gt= new GainTime();
		Long startTime=details.getStartTime();
		Long length=details.getLength();
		Long takeoff=details.getTakeoff();
		PlaneUtil pu= new PlaneUtil();
		Map<String, Object> mapp = new HashMap<>();
		mapp=pu.time(startTime,takeoff,length);
		Date b= new Date(startTime);
		calendar.setTime(b);
		int hour=calendar.get(Calendar.HOUR_OF_DAY);
		List<KWord> ls=new ArrayList<>();
		ls= skySetService.queryByKWord(details.getNeed());
		List<KWord> ping=new ArrayList<>();
		Map<String, Object> mapM = new HashMap<>();
		/**
		 * 匹配相似时间
		 */
		for (int i = 0; i < ls.size(); i++) {
			KWord str = ls.get(i);
			String ctime=gt.getDatime(str.getContent());
			String dateStr = "";
			if (StringUtil.isNotEmpty(ctime)) {
				Pattern p = Pattern.compile("(\\d{2})时");
				Matcher m = p.matcher(ctime);
				while (m.find()) {
					dateStr += m.group();
				}
			}
			Pattern p1 = Pattern.compile("[^0-9]");  
			Matcher m1 = p1.matcher(dateStr);
			if(m1.replaceAll("").trim()==""){
				mapM.put("warn",mapp);
				return CommonResp.buildSuccessResp(mapM).toJsonString();
			}
			int a=Integer.parseInt(m1.replaceAll("").trim());
			if(a-2<hour&&hour<a+2){
				ping.add(ls.get(i));
			}
		}
		mapM.put("kword",ping);
		mapM.put("warn",mapp);
		return CommonResp.buildSuccessResp(mapM).toJsonString();
	}

	
	/**
	 * 匹配结果
	 */
	 
	@ResponseBody
	@RequestMapping(value = "search")
	public String search(String kwords, HttpSession session) throws ParseException {
		List<Criterion> criterions = new ArrayList<>();
		if (StringUtils.isNoneBlank(kwords)) {
			criterions.add(Restrictions.ilike("kwords", "%" + kwords + "%"));
		}
		Criterion criterion = Restrictions.and(criterions.toArray(new Criterion[] {}));
		DetachedCriteria queryListCriteria = ExtraSpringHibernateTemplate.C.build(poc, Order.desc("kwordId"), criterion);
		DetachedCriteria queryCountCriteria = ExtraSpringHibernateTemplate.C.buildRowCount(poc, criterion);
		Paging<?> paging = extraSpringHibernateTemplate.findPageByCriteria(queryListCriteria, queryCountCriteria, 0, Integer.MAX_VALUE);
		return CommonResp.buildSuccessResp(paging).toJsonString();
	}

}
